package com.seveneight.baijiesteward.ui.main.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.bean.BankListBean;
import com.seveneight.baijiesteward.utils.StringUtils;
import com.seveneight.baijiesteward.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 信用卡申请
 */
public class CreditCardApplicationdActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.bank_photo1)
    ImageView bank_photo1;
    @BindView(R.id.bank_photo2)
    ImageView bank_photo2;
    @BindView(R.id.bank_photo3)
    ImageView bank_photo3;
    @BindView(R.id.theme_name)
    TextView theme_name;
    @BindView(R.id.label_name)
    TextView label_name;
    Intent intent;
    List<BankListBean> bankList;
    List<BankListBean> themeList;
    List<BankListBean> tableList;
    String[] bankArray;
    String[] themeArray;
    String[] tableArray;

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_credit_card_applicationd);

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        tv_title.setText("信用卡申请");
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){

            switch (requestCode){
                case 111:
                    bankList = (List<BankListBean>) data.getSerializableExtra("selData");
                    setBank();
                    break;
                case 112:
                    themeList= (List<BankListBean>) data.getSerializableExtra("selData");
                    setTheme();
                    break;
                case 113:
                    tableList= (List<BankListBean>) data.getSerializableExtra("selData");
                    settable();
                    break;
            }
        }
    }
    public void setBank(){
        bank_photo2.setVisibility(View.GONE);
        bank_photo3.setVisibility(View.GONE);
        if (bankList.size()>0){
            Glide.with(this).load(bankList.get(0).getIconPath()).into(bank_photo1);
        }
        if (bankList.size()>1){
            bank_photo2.setVisibility(View.VISIBLE);
            Glide.with(this).load(bankList.get(1).getIconPath()).into(bank_photo2);
        }
        if (bankList.size()>2){
            bank_photo3.setVisibility(View.VISIBLE);
            Glide.with(this).load(bankList.get(2).getIconPath()).into(bank_photo3);
        }
        bankArray=new String[bankList.size()];
        for (int i=0;i<bankList.size();i++){
            bankArray[i]=bankList.get(i).getId();
        }
    }
    public void setTheme(){
        themeArray=new String[themeList.size()];
        String theme="";
        for (int i=0;i<themeList.size();i++){
            if (StringUtils.isEmpty(theme)){
                theme=themeList.get(i).getName();
            }else {
                theme+="、"+themeList.get(i).getName();
            }
            themeArray[i]=themeList.get(i).getId();
        }
        theme_name.setText(theme);
    }
    public void settable(){
        tableArray=new String[tableList.size()];
        String table="";
        for (int i=0;i<tableList.size();i++){
            if (StringUtils.isEmpty(table)){
                table=tableList.get(i).getName();
            }else {
                table+="、"+tableList.get(i).getName();
            }
            tableArray[i]=tableList.get(i).getId();
        }
        label_name.setText(table);
    }


    @OnClick({R.id.iv_back,R.id.credit_bt_next,R.id.bank_layout,R.id.theme_name,R.id.label_name})
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.credit_bt_next:///下一步
                if (null == bankList){
                    ToastUtil.show(this,"请选择银行");
                    return;
                }
                if (null == themeList){
                    ToastUtil.show(this,"请选择主题");
                    return;
                }
                if (null == tableList){
                    ToastUtil.show(this,"请选择主题");
                    return;
                }
                intent=new Intent(this,SelectApplicationBankCardActivity.class);
                intent.putExtra("bank",bankArray);
                intent.putExtra("theme",themeArray);
                intent.putExtra("table",tableArray);
                startActivity(intent);
                finish();
                break;
            case R.id.bank_layout:///选择银行
                intent=new Intent(this,SelectBankActivity.class);
                intent.putExtra("title_name","选择银行");
                startActivityIfNeeded(intent,111);

                break;
            case R.id.theme_name:///选择主题
                intent=new Intent(this,SelectBankActivity.class);
                intent.putExtra("title_name","选择主题");
                startActivityIfNeeded(intent,112);
                break;
            case R.id.label_name:///选择标签
                intent=new Intent(this,SelectThemeActivity.class);
                startActivityIfNeeded(intent,113);
                break;
        }

    }
}
